package com.ntschy.common.core;

import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageInfo;
import com.ntschy.common.constant.HttpStatus;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;

/**
 * 操作消息提醒
 *
 * @author ezreal_geng
 */
@Getter
@Setter
@ApiModel(description = "接口返回数据")
public class AjaxResult<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    @ApiModelProperty(value = "业务状态码")
    private Integer code;

    @ApiModelProperty(value = "返回消息")
    private String msg;

    @ApiModelProperty(value = "数据对象")
    private T data;

    @ApiModelProperty(value = "请求状态码")
    private boolean success;

    /**
     * 初始化一个新创建的 AjaxResult 对象，使其表示一个空消息。
     */
    public AjaxResult() {
    }

    /**
     * 初始化一个新创建的 AjaxResult 对象
     *
     * @param code 状态码
     * @param msg  返回内容
     * @param data 数据对象
     */
    public AjaxResult(boolean success, int code, String msg, T data) {
        this.success = success;
        this.code = code;
        this.msg = msg;
        if (ObjectUtil.isNotNull(data)) {
            this.data = data;
        }
    }

    /**
     * 返回成功消息
     */
    public static AjaxResult<String> success(String msg) {
        return new AjaxResult<>(true, HttpStatus.SUCCESS, msg, null);
    }

    /**
     *? 数据列表返回成功消息
     */
    public static AjaxResult success(Object o) {
        return new AjaxResult(true, HttpStatus.SUCCESS, null, o);
    }

    /**
     * 返回成功消息
     */
    public static <T> AjaxResult<T> success(String msg, T data) {
        return new AjaxResult<>(true, HttpStatus.SUCCESS, msg, data);
    }


    /**
     * 返回错误消息
     */
    public static AjaxResult<String> error(int code, String msg) {
        return new AjaxResult<>(false, code, msg, null);
    }

    /**
     * 参数校验返回错误消息
     */
    public static AjaxResult<String> error(String msg) {
        return new AjaxResult<>(false, HttpStatus.BAD_REQUEST, msg, null);
    }
}
